//
//  MyAddressViewController.m
//  BaseFrame
//
//  Created by 孟学浩 on 2017/5/11.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "MyAddressViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

#define ROUND_WIDTH 30

@interface MyAddressViewController () <MKMapViewDelegate,CLLocationManagerDelegate>
{
    CLLocationManager *_locationManager;
    
    CLLocationCoordinate2D centerCoordinate;
}

@property CLLocationDegrees latitudeDelta;

@property (nonatomic , strong) UIButton *theCurrentPosition;                        /**<当前位置*/
@property (nonatomic , strong) MKMapView *mapView;

@property (nonatomic , strong) UIBarButtonItem *theNextStepBarButton;               //下一步

@end

@implementation MyAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"搜索店铺";
    [self.view addSubview:self.mapView];
    self.navigationItem.rightBarButtonItem = self.theNextStepBarButton;
    _locationManager = [[CLLocationManager alloc]init];
    
    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
    if (status == kCLAuthorizationStatusDenied || status == kCLAuthorizationStatusRestricted) {
        NSLog(@">>>>>>>>>> 位置服务不可用 <<<<<<<<<<<<");
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"您的位置服务当前不可用，请打开位置服务后重试" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil];
        [alertController addAction:okAction];
        [self presentViewController:alertController animated:YES completion:nil];
    }
    
    if(![CLLocationManager locationServicesEnabled]||[CLLocationManager authorizationStatus]!=kCLAuthorizationStatusAuthorizedWhenInUse)
    {
        [_locationManager requestWhenInUseAuthorization];
    }
    _locationManager.delegate = self;
    //     设置定位精度
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    
}

#pragma mark - init


/**
 *  地图初始化
 */
- (MKMapView *)mapView{
    if (!_mapView) {
        _mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 )];
        _mapView.delegate = self;
        [_mapView addSubview:self.theCurrentPosition];
        _mapView.userTrackingMode = MKUserTrackingModeFollow;
        _mapView.mapType = MKMapTypeStandard;
        //是否显示交通流量
        _mapView.showsTraffic =YES;
        //旋转地图时是否显示指南针
        _mapView.showsCompass =YES;
        //是否在地图上显示比例尺
        _mapView.showsScale =YES;
        
    }
    return _mapView;
}


#pragma mark - CLLocationManager delegate
//定位触发
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    
    NSLog(@"经度%f,纬度%f",newLocation.coordinate.longitude, newLocation.coordinate.latitude);
    
    if(self.mapView.userTrackingMode != MKUserTrackingModeFollow)
    {
        [self.mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
    }
    
    //取消定位
    [manager stopUpdatingLocation];
    
}

@end
